package com.zoe.thread.th14_volatile测试;

/**
 * @Author Zoe
 * @Date: 2021/05/07
 * @Description Volatile 不保证原子性; 可以使用Atomic保证原子性
 */
public class VolatileAtomicTest {
//    public static AtomicInteger num = new AtomicInteger();
    public static int num = 0;

    public  static void add() {
//        num.getAndIncrement();
        num++;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            new Thread(() -> {
                for (int i1 = 0; i1 < 1000; i1++) {
                    add();
                }
            }, i + "").start();
        }

        while (Thread.activeCount()>2){
            Thread.yield();
        }

        System.out.println(num);
    }
}
